package com.dev.security.manager.controllers;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.security.login.util.LoginConstants;
import com.dev.security.manager.beans.Source;
import com.dev.security.manager.service.ISourceService;
import com.dev.security.util.DataGrid;
import com.dev.security.util.Response;

/**
 * @author 潜行的青衣
 * @see
 */
@Controller
@RequestMapping("/manager/source/")
public class SourceController {

	@Resource
	private ISourceService sourceService;
	
	private final String[] types = new String[]{
			LoginConstants.TYPE_NEED_LOGIN,
			LoginConstants.TYPE_NEED_ROOT};
	
	@RequestMapping("browse")
	public @ResponseBody DataGrid<Source> browse(String per_id, int page, int rows) {
		return new DataGrid<Source>(this.sourceService.findPager(per_id, page, rows));
	}
	
	@RequestMapping("add")
	public @ResponseBody Response add(Source source) {
		if(this.sourceService.save(source)) {
			return new Response(true);
		} else {
			return new Response("已经存在此资源，无法添加！");
		}
	}
	
	@RequestMapping("remove")
	public @ResponseBody Response remove(String[] uris) {
		this.sourceService.remove(uris);
		return new Response(true);
	}
	
	@RequestMapping("extra/browse")
	public @ResponseBody DataGrid<Source> extraBrowse(int page, int rows) {
		return new DataGrid<Source>(this.sourceService.findPager(types, page, rows));
	}
	
	@RequestMapping("extra/add")
	public @ResponseBody Response extraAdd(Source source) {
		if(this.sourceService.save(source)) {
			return new Response(true);
		} else {
			return new Response("已经存在此资源，无法添加！");
		}
	}
	
	@RequestMapping("extra/remove")
	public @ResponseBody Response extraRemove(String[] uris) {
		this.sourceService.remove(uris, types);
		return new Response(true);
	}
}
